Skip to content

perf: Speed up IN segment condition evaluation#295

Merged
khvn26 merged 3 commits intomainfrom
perf/in-segment-condition
Apr 24, 2026
Merged

perf: Speed up IN segment condition evaluation#295
khvn26 merged 3 commits intomainfrom
perf/in-segment-condition

Conversation

@khvn26
Copy link
Copy Markdown
Member

@khvn26 khvn26 commented Apr 24, 2026

  • Extract IN-condition value resolution into _get_in_values() and cache the string-form parse with lru_cache, returning a frozenset for O(1) membership.
  • Skip the json.loads attempt entirely when the value doesn't start with [ — plain comma-separated strings go straight to split(","), avoiding a try/except on every first evaluation.
  • Add three IN-condition contexts (JSON array form, comma-separated, native list) to the codspeed benchmark so future changes to IN stay measured.
  • Bump the engine-test-data submodule to v3.6.0, which adds the bracketed-invalid-JSON CSV fallback case (feat: Add IN condition bracketed-invalid-JSON CSV fallback test engine-test-data#50) that covers the new except ValueError branch in _parse_in_values_str.

Local wall-clock: IN-only eval ~2.29 μs → ~1.58 μs per call (~30% faster) across the benchmark contexts; cold-path parse ~2.1× faster on non-JSON strings.

Cache the parsed IN-condition value set via lru_cache on the str path
and use a frozenset for membership checks. Also short-circuit the JSON
parse attempt for values that don't start with "[" — plain comma-
separated strings skip json.loads entirely.

Adds IN-condition contexts (JSON array form, comma-separated, and
native list) to the codspeed benchmark so future changes to IN stay
measured.

beep boop
@khvn26 khvn26 requested a review from a team as a code owner April 24, 2026 09:35
@khvn26 khvn26 requested review from Zaimwa9 and removed request for a team April 24, 2026 09:35
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 24, 2026

File Coverage Missing
All files 100%

Minimum allowed coverage is 100%

Generated by 🐒 cobertura-action against fbc5641

@khvn26 khvn26 marked this pull request as draft April 24, 2026 09:37
 HEAD

Temporary pin so CI can run against the bracketed-invalid-JSON CSV
fallback test case that covers the new `except ValueError` branch in
`_parse_in_values_str`. Will be bumped to the next release tag once
Flagsmith/engine-test-data#50 merges.

beep boop
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 24, 2026

Merging this PR will not alter performance

✅ 1 untouched benchmark


Comparing perf/in-segment-condition (fbc5641) with main (6237c31)

Open in CodSpeed

Moves the submodule off the temporary PR-branch pin to the tagged
release that includes the bracketed-invalid-JSON CSV fallback test
case (Flagsmith/engine-test-data#50).

beep boop
@khvn26 khvn26 marked this pull request as ready for review April 24, 2026 10:05
Copy link
Copy Markdown
Contributor

@Zaimwa9 Zaimwa9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving as I have an unsure minor defensive comment

Comment thread flag_engine/segments/evaluator.py
@khvn26 khvn26 requested a review from Zaimwa9 April 24, 2026 11:33
@khvn26 khvn26 merged commit 1d8b273 into main Apr 24, 2026
14 checks passed
@khvn26 khvn26 deleted the perf/in-segment-condition branch April 24, 2026 11:50
khvn26 added a commit to Flagsmith/flagsmith-python-client that referenced this pull request Apr 24, 2026
Picks up the IN segment-condition evaluation speedup
(Flagsmith/flagsmith-engine#295), which cuts per-IN-condition latency
on segment walks by roughly 30%. Complementary to the lazy identity
evaluation added in this PR — most customer envs will benefit from both.

beep boop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants